package com.pur.zsms.supplier.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.supplier.service.ISupplierOrderService;
import com.pur.zsms.supplier.service.ISupplierService;

@Controller
@RequestMapping(value = "/supCheck")
public class SupplierOrderCheckController {
	@Resource(name = "supplierOrderService")
	private ISupplierOrderService supplierOrderService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "storeService")
	private IStoreService storeService;

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SupplierController.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@RequestMapping(value = "/findSupCheck.do")
	@ResponseBody
	public String findSupCheck(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, OrderSupplierVo orderSupplierVo, HttpSession session)
	        throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		// 根据用户id得出supid
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		orderSupplierVo.setOrdSupId(supId);
		if (orderSupplierVo.getInitFlag() == 0) {
			json.put("success", false);
			return json.toString();
		}

		this.pageList = supplierOrderService.findSupCheck(pageController, orderSupplierVo);
		List<OrderSupplierVo> orderSupplierVoList = new ArrayList<OrderSupplierVo>();
		List<OrderSupplierVo> orderSupplierVoListFooter = new ArrayList<OrderSupplierVo>();
		BigDecimal ordValueReal = new BigDecimal(0);
		BigDecimal ordValueRealCost = new BigDecimal(0);
		BigDecimal turnover = new BigDecimal(0);

		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierVo f = new OrderSupplierVo();

				f.setOid((Integer) item[0]);
				f.setOrdId((Integer) item[1]);
				f.setOrdCustId((Integer) item[2]);
				f.setOrdSupId((Integer) item[3]);
				f.setOrdState((Integer) item[4]);
				f.setOrdDate((String) item[5]);
				f.setOrdDay((String) item[6]);
				f.setOrdValue((BigDecimal) item[9]);
				f.setOrdCustName((String) item[10]);
				f.setOrdSupName((String) item[11]);
				f.setOrdPaymentState((Integer) item[12]);
				if (item[13] != null) {
					f.setOrdValueReal(new BigDecimal(item[13].toString()));
					ordValueReal = ordValueReal.add(new BigDecimal(item[13].toString()));
				} else {
					f.setOrdValueRealCost(null);
				}

				f.setCommitDeliverState((Integer) item[14]);
				if (item[15] != null) {
					f.setOrdValueRealCost(new BigDecimal(item[15].toString()));
					ordValueRealCost = ordValueRealCost.add(new BigDecimal(item[15].toString()));
				} else {
					f.setOrdValueRealCost(null);
				}
				if (item[16] != null) {
					f.setGrossMargin(item[16].toString() + "%");
				} else {
					f.setGrossMargin("无");
				}
				if (item[17] != null) {
					f.setTurnover(new BigDecimal(item[17].toString()));
					turnover = turnover.add(new BigDecimal(item[17].toString()));
				} else {
					f.setTurnover(new BigDecimal(0));
				}
				f.setContent("查看详细清单");
				orderSupplierVoList.add(f);

			}
		}
		OrderSupplierVo footer = new OrderSupplierVo();
		footer.setOrdCustName("合计");
		footer.setOrdValueRealCost(ordValueRealCost);
		footer.setOrdValueReal(ordValueReal);
		footer.setTurnover(turnover);
		String grossMargin = "无";
		if (ordValueReal.compareTo(new BigDecimal(0)) != 0) {
			grossMargin = (ordValueReal.subtract(ordValueRealCost)).multiply(new BigDecimal(100))
			        .divide(ordValueReal, 2, BigDecimal.ROUND_HALF_UP).toString()
			        + "%";
		}
		footer.setGrossMargin(grossMargin);
		orderSupplierVoListFooter.add(footer);
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierVoList);
		json.put("footer", orderSupplierVoListFooter);

		return json.toString();
	}
}
